package com.itheima.ydd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.ydd.common.R;
import com.itheima.ydd.dto.PageDto;
import com.itheima.ydd.dto.SysUserDto;
import com.itheima.ydd.dto.UserInfo;
import com.itheima.ydd.entity.*;
import com.itheima.ydd.mapper.SysUserMapper;
import com.itheima.ydd.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author author
 * @since 2022-05-22
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements ISysUserService {
    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private ISysRoleService sysRoleService;

    @Autowired
    private ISysMenuService sysMenuService;

    @Autowired
    private ISysRoleMenuService roleMenuService;

    @Autowired
    private ISysDictService sysDictService;

   /* @Override
    public R profile(Long userId) {
        //1.1 查询登录用户信息
        SysUser sysUser = sysUserService.getById(userId);
        //1.2 填入用户角色信息
        LambdaQueryWrapper<SysRole> sysRoleWrapper = new LambdaQueryWrapper<>();
        String sysUserRoleId = String.valueOf(sysUser.getRoleId());
        sysRoleWrapper.eq(SysRole::getId,sysUserRoleId.substring(2, sysUserRoleId.length() - 2));
//        sysRoleWrapper.select(SysRole::getName);
        //1.2.1 查出对应的角色详细信息
        List<String> roleNameList = new ArrayList<>();
        SysRole sysRole = sysRoleService.getOne(sysRoleWrapper);
        //1.2.2 封装进sysUserDto对象中
        SysUserDto userInfo = new SysUserDto();
        BeanUtils.copyProperties(sysUser,userInfo);
        roleNameList.add(sysRole.getName());
        userInfo.setRoleName(roleNameList);


        //2 查菜单数据
        LambdaQueryWrapper<SysMenu> sysMenuWrapper = new LambdaQueryWrapper<>();
        sysMenuWrapper.ne(SysMenu::getPath,"");
        List<SysMenu> menuList = sysMenuService.list(sysMenuWrapper);

        //3 dictsList
        LambdaQueryWrapper<SysDict> sysDictWrapper = new LambdaQueryWrapper<>();
        sysDictWrapper.select(SysDict::getType);
        sysDictWrapper.groupBy(SysDict::getType);
        //3.1 分组查询所有dict_type
        List<Object> dictTypes = sysDictService.listObjs(sysDictWrapper);
        Map<String, Map<String,String>> dictsList = new HashMap<>();
        //3.2 遍历dictTypes ，查询对应的dict信息，存入dictsList中
        for (Object o : dictTypes) {
            Map<String, String> map = new HashMap<>();

            sysDictWrapper = new LambdaQueryWrapper<>();
            sysDictWrapper.eq(SysDict::getType,o);
            List<SysDict> sysDicts = sysDictService.list(sysDictWrapper);
            for (SysDict sysDict : sysDicts) {
                //3.3 将dict_value,dict_label存入集合
                map.put(sysDict.getValue(),sysDict.getLabel());
            }

            //3.4 将保存后的数据存入dictsList
            dictsList.put((String)o,map);
        }

        //4 准备 btnList
        Map<Integer,String> btnList = new HashMap<>();
        btnList.put(0,"*");

        //5 将所有查出的数据存入 R 中，返回
         Map<String, Object> map = new HashMap<>();


        map.put("btnList",btnList);
        map.put("dictsList",dictsList);
        map.put("menuList",menuList);
        map.put("userInfo",userInfo);

        return R.success(map);

    }*/

    @Override
    public PageDto getList(String loginName, String status, String phone, int page, int limit) {
        Page<SysUser> sysUserPage = new Page<>(page, limit);
        LambdaQueryWrapper<SysUser> userLambdaQueryWrapper = new LambdaQueryWrapper<>();
        userLambdaQueryWrapper.like(StringUtils.isNotEmpty(loginName),SysUser::getLoginName,loginName);
        userLambdaQueryWrapper.eq(StringUtils.isNotEmpty(status),SysUser::getStatus,status);
        userLambdaQueryWrapper.like(StringUtils.isNotEmpty(phone),SysUser::getPhone,phone);
        userLambdaQueryWrapper.eq(SysUser::getDelFlag,"0");
        this.page(sysUserPage,userLambdaQueryWrapper);
        PageDto pageDto = new PageDto();
        BeanUtils.copyProperties(sysUserPage,pageDto,"records");

        List<SysUser> records = sysUserPage.getRecords();
        List<SysUserDto> userDtos=records.stream().map((item)->{
            SysUserDto sysUserDto = new SysUserDto();
            BeanUtils.copyProperties(item,sysUserDto);
            List<String> roleName = new ArrayList<>();
          /*  String roleId = item.getRoleId()
                    .replace("[","")
                    .replace("]", "")
                    .replace("\"", "");*/
            List<String> roleIds = item.getRoleId();

            LambdaQueryWrapper<SysRole> roleLambdaQueryWrapper = new LambdaQueryWrapper<>();
            roleLambdaQueryWrapper.in(SysRole::getId,roleIds);
            List<SysRole> sysRoles = sysRoleService.list(roleLambdaQueryWrapper);
            for (SysRole sysRole : sysRoles) {
                roleName.add(sysRole.getName());
            }

                sysUserDto.setRoleName(roleName);

            return sysUserDto;
        }).collect(Collectors.toList());

        pageDto.setRecords(userDtos);
        return pageDto;
    }

    @Override
    public SysUserDto getByIdWhisRole(String id) {
        //根据id查询基本信息
        SysUser sysUser = this.getById(id);
        //数据拷贝
        SysUserDto sysUserDto = new SysUserDto();
        BeanUtils.copyProperties(sysUser,sysUserDto);
      /*  String roleId = sysUser.getRoleId()
                .replace("[","")
                .replace("]", "")
                .replace("\"", "");*/
        List<String> roleIds = sysUser.getRoleId();

        List<String> roleName = new ArrayList<>();
        LambdaQueryWrapper<SysRole> roleLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roleLambdaQueryWrapper.in(SysRole::getId,roleIds);
        List<SysRole> sysRoles = sysRoleService.list(roleLambdaQueryWrapper);
        for (SysRole sysRole : sysRoles) {
            roleName.add(sysRole.getName());

        }

        if (roleName!=null){
            sysUserDto.setRoleName(roleName);
        }

        return sysUserDto;
    }

    @Transactional
    public void detete(String id) {
        LambdaUpdateWrapper<SysUser> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(SysUser::getDelFlag, "1");
        wrapper.eq(SysUser::getId, id);
        sysUserService.update(wrapper);
    }
}
